package com.example.dbmysql.Controller;


import com.example.dbmysql.pojo.LadderControl;
import com.example.dbmysql.result.Result;
import com.example.dbmysql.service.LadderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/ladder")
public class LadderController {
    @Autowired
    private LadderService ladderService;

    @PostMapping("/add")
    public Result add(@RequestBody LadderControl ladderControl){

        LadderControl  ladder =  ladderService.selectByfloorAndpublic(ladderControl.getPublicId(),ladderControl.getLadderId());

        if(ladder != null) return Result.error("请勿重复添加");

        ladderService.add(ladderControl);

        return  Result.success();

    }

    @GetMapping("/getAll")
    public Result getAll(){

        List<LadderControl> list = ladderService.selectAll();

        return Result.success(list);
    }

    @GetMapping("/getById/{id}")
    public Result getById(@PathVariable("id") Integer id){

        LadderControl  ladder =  ladderService.selectById(id);

        return Result.success(ladder);
    }

    @PutMapping("/update")
    public Result updateById(@RequestBody LadderControl ladderControl){

        LadderControl  ladder =  ladderService.selectByfloorAndpublic(ladderControl.getPublicId(),ladderControl.getLadderId());

        if(ladder != null) return Result.error("请勿重复添加");

        ladderService.update(ladderControl);

        return  Result.success();
    }

    @DeleteMapping("/del/{id}")
    public Result delById(@PathVariable("id") Integer id){

        LadderControl  ladder =  ladderService.selectById(id);

        if(ladder == null) return Result.error("删除数据不存在");

         ladderService.delById(id);

        return Result.success();

    }
}
